using System.Xml.Linq;

namespace TDUnit.TestDataParsers.XmlParser
{
    public abstract class ElementParserBase
    {
        protected static bool ParseAttributeAsBoolean(XAttribute attribute, string attributeName)
        {
            if (attribute == null)
                return false;
            bool autoIdent;

            if (attribute.Value.Equals("1")) return true;
            if (attribute.Value.Equals("0")) return false;

            if (!bool.TryParse(attribute.Value, out autoIdent))
                throw new XmlTestDataParserException(
                    string.Format("Could not parse value '{0}'. {1} should be set to 'True' or 'False' if specified.", attribute.Value, attributeName), attribute);

            return autoIdent;
        }
    }
}